package larpplanner.gui;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;

public class AdminWin extends org.eclipse.swt.widgets.Composite {
	
	private GUISafeLARPManager mngr = GUISafeLARPManager.get();
	
	private Button dltBtn;
	private Button importBtn;
	private Button dltCharBtn;
	private Button addCharBtn;
	
	public void open() {
		Shell shell = getShell();
		Point size = getSize();
        shell.setLayout(new FillLayout());
        shell.setText("LARP Planner Administrator");
        shell.layout();
        if(size.x == 0 && size.y == 0) {
            pack();
            this.setSize(734, 108);
            shell.pack();
        } else {
            Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
            shell.setSize(shellBounds.width, shellBounds.height);
        }
        shell.open();
        Display display = shell.getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }

	public AdminWin(Composite parent, int style) {
		super(parent, style);
		initGUI();
		initLogic();
	}
	
	private void initLogic() {
		
		dltBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				PopMessage.notifyMessage("deleting all..");
				mngr.deleteAll(new SafeCallback<Boolean>() {
					public void onReturn(Boolean result) {
						PopMessage.notifyMessage(result ? "DB deleted successfully" : "DB delete failed");
					}
				});
			}
		});
		
		importBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				PopMessage.notifyMessage("updaing DB..");
				mngr.updateDB(new SafeCallback<Boolean>() {
					public void onReturn(Boolean result) {
						PopMessage.notifyMessage(result ? "DB updated successfully" : "DB update failed");
					}
				});
			}
		});
		
		dltCharBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				CharDltWin charDltWin = new CharDltWin(getShell(), SWT.NONE);
				charDltWin.open();
			}
		});
		
		addCharBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				NewCharWin newCharWin = new NewCharWin(getShell(), SWT.NONE);
				newCharWin.open();
			}
		});
	}

	private void initGUI() {
		try {
			//this.setSize(350, 70);
			GridLayout thisLayout = new GridLayout();
			this.setSize(350, 65);
			thisLayout.numColumns = 2;
			this.setLayout(thisLayout);
			{
				dltBtn = new Button(this, SWT.PUSH | SWT.CENTER);
				GridData dltBtnLData = new GridData();
				dltBtnLData.horizontalAlignment = GridData.FILL;
				dltBtnLData.grabExcessHorizontalSpace = true;
				dltBtn.setLayoutData(dltBtnLData);
				dltBtn.setText("Delete All");
			}
			{
				dltCharBtn = new Button(this, SWT.PUSH | SWT.CENTER);
				GridData dltCharBtnLData = new GridData();
				dltCharBtnLData.horizontalAlignment = GridData.FILL;
				dltCharBtnLData.grabExcessHorizontalSpace = true;
				dltCharBtn.setLayoutData(dltCharBtnLData);
				dltCharBtn.setText("Delete Character");
			}
			{
				importBtn = new Button(this, SWT.PUSH | SWT.CENTER);
				GridData importBtnLData = new GridData();
				importBtnLData.horizontalAlignment = GridData.FILL;
				importBtnLData.grabExcessHorizontalSpace = true;
				importBtn.setLayoutData(importBtnLData);
				importBtn.setText("Massive Import");
			}
			{
				addCharBtn = new Button(this, SWT.PUSH | SWT.CENTER);
				GridData addCharBtnLData = new GridData();
				addCharBtnLData.horizontalAlignment = GridData.FILL;
				addCharBtnLData.grabExcessHorizontalSpace = true;
				addCharBtn.setLayoutData(addCharBtnLData);
				addCharBtn.setText("Add Character");
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		AdminWin inst = new AdminWin(shell, SWT.NONE);
		inst.open();
	}

}
